package com.itfreer.gis.cad.analysis;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.sound.sampled.Line;

import org.kabeja.dxf.DXFBlock;
import org.kabeja.dxf.DXFCircle;
import org.kabeja.dxf.DXFConstants;
import org.kabeja.dxf.DXFDocument;
import org.kabeja.dxf.DXFEntity;
import org.kabeja.dxf.DXFLWPolyline;
import org.kabeja.dxf.DXFLayer;
import org.kabeja.dxf.DXFLine;
import org.kabeja.dxf.DXFPolyline;
import org.kabeja.dxf.DXFVertex;
import org.kabeja.dxf.helpers.Point;
import org.kabeja.dxf.objects.DXFDictionary;
import org.kabeja.parser.DXFParser;
import org.kabeja.parser.ParseException;
import org.kabeja.parser.Parser;
import org.kabeja.parser.ParserBuilder;
import org.springframework.stereotype.Component;

import com.itfreer.gis.analysis.IGisFileAnalysis;
import com.itfreer.gis.graphic.GisCircle;
import com.itfreer.gis.graphic.GisGraphic;
import com.itfreer.gis.graphic.GisLine;
import com.itfreer.gis.graphic.GisPoint;
import com.itfreer.gis.graphic.GisPolyg;

@Component("dxf")
public class DxfAnalysis implements IGisFileAnalysis{
	/**
	 * dxf文件解析
	 * @return
	 * @throws ParseException
	 */
	public List<GisGraphic> analysis(String filePath) throws Exception {
		
		List<GisGraphic> resultLst=new ArrayList<GisGraphic>();
		
		Parser parser = ParserBuilder.createDefaultParser();
		//parser.parse("D:\\CAD文件\\test02.dxf", DXFParser.DEFAULT_ENCODING);
		parser.parse(filePath);
		DXFDocument doc = parser.getDocument();
		Iterator in=doc.getDXFLayerIterator();
		while(in.hasNext()) {
			DXFLayer layer=(DXFLayer) in.next();
			//1、读取多边形对象
			List<DXFLWPolyline>lst=layer.getDXFEntities(DXFConstants.ENTITY_TYPE_LWPOLYLINE);
			if(lst!=null && lst.size()>0) {
				for (int index = 0; index < lst.size(); index++) {
					//这里需要处理掉是否闭合，闭合的采用面要素，非闭合的采用线要素
					//（闭合分两种情况，1、闭合的集合元素，如矩形，2、首位两个点坐标的一样的元素）
					//GisPolyg polyg=new GisPolyg();
					List<GisPoint> points=new ArrayList<GisPoint>();
					DXFLWPolyline line=lst.get(index);
					Iterator vertexs=line.getVertexIterator();
					while(vertexs.hasNext()) {
						DXFVertex vertex=(DXFVertex)vertexs.next();
						Point point=vertex.getPoint();
						point.getX();
						
						GisPoint gisPoint=new GisPoint();
						gisPoint.setX(point.getX());
						gisPoint.setY(point.getY());
						gisPoint.setZ(point.getZ());
						points.add(gisPoint);
					}
					
					if(points!=null && points.size()>1) {
						if(line.getFlags()==1
								|| points.get(0).equals(points.get(points.size()-1))) {
							GisPolyg polyg=new GisPolyg();
							polyg.addPoints(points);
							polyg.setGeometry();
							resultLst.add(polyg);
						}else {
							GisLine gisLine=new GisLine();
							gisLine.addPoints(points);
							gisLine.setGeometry();
							resultLst.add(gisLine);
						}
					}
		        }
			}
			
			
			//2、读取线对象
			List<DXFLine>lstLine=layer.getDXFEntities(DXFConstants.ENTITY_TYPE_LINE);
			if(lstLine!=null && lstLine.size()>0) {
				for (int index = 0; index < lstLine.size(); index++) {
					
					DXFLine line=(DXFLine) lstLine.get(index);
					GisLine gisLine=new GisLine();
					//简单的线处理
					Point spoint=line.getStartPoint();
					GisPoint gisSPoint=new GisPoint();
					gisSPoint.setX(spoint.getX());
					gisSPoint.setY(spoint.getY());
					gisSPoint.setZ(spoint.getZ());
					gisLine.addPoint(gisSPoint);
					//简单的线处理
					Point epoint=line.getEndPoint();
					GisPoint gisEPoint=new GisPoint();
					gisEPoint.setX(epoint.getX());
					gisEPoint.setY(epoint.getY());
					gisEPoint.setZ(epoint.getZ());
					gisLine.addPoint(gisEPoint);
					
					gisLine.setGeometry();
					resultLst.add(gisLine);
		        }
			}
			
			//3、读取圆对象
			List<DXFCircle>lstCircle=layer.getDXFEntities(DXFConstants.ENTITY_TYPE_CIRCLE);
			if(lstCircle!=null && lstCircle.size()>0) {
				for (int index = 0; index < lstCircle.size(); index++) {
					
					DXFCircle circle=(DXFCircle) lstCircle.get(index);
					GisCircle gisCircle=new GisCircle();
					
					Point cpoint=circle.getCenterPoint();
					GisPoint gissPoint=new GisPoint();
					gissPoint.setX(cpoint.getX());
					gissPoint.setY(cpoint.getY());
					gissPoint.setZ(cpoint.getZ());
					gisCircle.setCenter(gissPoint);
					gisCircle.setRadius(circle.getRadius());
					
					gisCircle.setGeometry();
					resultLst.add(gisCircle);
		        }
			}
		}
		System.out.println("结束");
		return resultLst;
	}

	
	private static void output(Object o){
		if(o==null){
			throw new IllegalArgumentException("params can not be null");
		}
		if(o instanceof Map){
			for(Object key:((Map) o).keySet()){
				System.out.println(key);
				output(((Map)o).get(key));
			}
		}else if(o instanceof Iterator){
			Iterator it=(Iterator)o;
			while(it.hasNext()){
				output(it.next());
			}
		}else if(o instanceof DXFDictionary){
			DXFDictionary d=(DXFDictionary)o;
			System.out.println(d.getID()+":"+d.getObjectType());
			if(d.getDXFObjectIterator()!=null){
				output(d.getDXFObjectIterator());
			}
		}else if(o instanceof DXFBlock){
			DXFBlock d=(DXFBlock)o;

			System.out.println(d.getName()+":"+d.getBounds().getWidth()+","+d.getBounds().getHeight());
		}else if(o instanceof DXFLayer){
			DXFLayer d=(DXFLayer)o;
			System.out.println(d.getName());
		}else if(o instanceof DXFEntity){
			DXFEntity d=(DXFEntity)o;
			System.out.println("ID:"+d.getID()+" Type:"+d.getType()+"  Color:"+d.getColor());
		if(d instanceof DXFLine){
			System.out.println("startPoint:");
			output(((DXFLine)d).getStartPoint());
			System.out.println("endPoint:");
			output(((DXFLine)d).getEndPoint());
		}else if(d instanceof DXFPolyline){
			DXFPolyline dpl=(DXFPolyline)d;
			output(dpl.getVertexIterator());
		}
		}else if(o instanceof DXFVertex){
			DXFVertex d=(DXFVertex)o;
			System.out.println("type:"+d.getType());
			System.out.println("point:");
			output(d.getPoint());
		}else if(o instanceof org.kabeja.dxf.helpers.Point){
			org.kabeja.dxf.helpers.Point
			p=(org.kabeja.dxf.helpers.Point)o;

			System.out.println("["+p.getX()+","+p.getY()+","+p.getZ()+"]");
		}else{
			System.out.println(o.toString());
		}
	}
}
